<?php
include('./includes/config.inc');
include('./includes/variables.inc');
include('./includes/libccramp.inc');
?>
<html>
<head>
<title></title>
<style>
<?php include('./themes/'.$theme.'/sidebar.css') ?>
.sidebarExpanded { font-size: 10; white-space: nowrap;}
</style>

<script type="text/javascript" src="./includes/javascript/dom-drag.js"></script>

</head>
<body onMouseDown="parent.destroyContext(); parent.document.getElementById('iframeContext').className = 'hidden' ">
<?php
$srchType = $_REQUEST[srchType];
$s = $_REQUEST[s];
if ($_REQUEST[cat]) $cat = $_REQUEST[cat];
if ($_REQUEST[status]) $status = $_REQUEST[status];

if ( ($srchType === 'art') || (!$srchType) || (!$srchType === 'all') ) {
   $searchWhere = " WHERE (`artist` LIKE '%".$_REQUEST['s']."%') "; 
   if ($status && ($status !== 'all' )) $searchWhere .= " AND `status` = '".$status."' ";
   else if ($rights < 2) $searchWhere .= " AND (`status`>'1') ";
   
   if ($_REQUEST[cat] && ($_REQUEST[cat] !== 'all')) $searchWhere .= " AND ( (`category` = '".$_REQUEST['cat']."') OR (`category2` = '".$_REQUEST['cat']."') OR (`category3` = '".$_REQUEST['cat']."') ) ";
   $query = mysql_query("
      SELECT DISTINCT(`artist`) 
      FROM `album` 
      $searchWhere
      ORDER BY TRIM(LEADING 'the ' FROM LOWER(`artist`)) ASC
   ");
   ?>
   <span style="font-weight: bold;">Artists:</span><br />
   <?php 
   $i = 0;
   while($s = mysql_fetch_array($query)) {
      $albQueryString = urlencode($s[artist]);
      ?>
      <span id="art[<?=$i?>]" class="sidebarCollapsed" >
      <!-- a onClick="parent.albInfoCtxt('art', '<?php echo(htmlspecialchars($albQueryString, ENT_QUOTES))?>')"><?php echo($s[artist]) ?></a -->
      <a onClick="parent.albInfoCtxtProto('artist||<?php echo(htmlspecialchars($albQueryString, ENT_QUOTES))?>||')"><?php echo($s[artist]) ?></a>
      
      </span><br />
      <?php
      $i++;
   }
   ?>
   <br /><span style="font-weight: bold; position: relative;">Compilations:</span><br />
   <?php
   $compQuery = "SELECT `cddbid`,`title` FROM `album` WHERE (`is_comp` = '1') ";
   if ($status) $compQuery .= " AND (`status` = '".$status."') ";
   else if ($rights < 2) $compQuery .= " AND (`status` > '1') ";
   if ($_REQUEST[cat] && ($_REQUEST[cat] !== 'all')) $compQuery .= " AND ( (`category` = '".$_REQUEST['cat']."') OR (`category2` = '".$_REQUEST['cat']."') OR (`category3` = '".$_REQUEST['cat']."') ) ";
   
   $compQuery = mysql_query($compQuery);
   while ($comp = mysql_fetch_array($compQuery)) {
      ?><span id="ar<?=$i?>" class="sidebarCollapsed" onClick="parent.albInfoCtxt('alb',<?=$comp[cddbid]?>)"><?php 
      $srchTrkQuery = mysql_query("SELECT COUNT(`trackno`) FROM `track` WHERE (`cddbid` = '".$comp[cddbid]."') AND (`artist`) LIKE '%".$_REQUEST['s']."%' ");
      $srchTrk = mysql_result($srchTrkQuery, 0);
      if ($srchTrk) {
         echo('('.$srchTrk . ')' . $comp[title].'<br />');
      }
      unset($srchTrk);
      ?></span><?php 
   }
} else if ( ($srchType === 'alb') || ($srchType === 'desc') ) {
   $albSql = " SELECT * FROM `album` WHERE ";
   if ($srchType === 'alb') {
      $albSqlWhere = " (`title` LIKE '%".mysql_real_escape_string($s)."%')  ";
      $albSqlOrderBy = " `title` ASC ";
   } else if ($srchType === 'desc') {
      $albSqlWhere = " (`review` LIKE '%".mysql_real_escape_string($s)."%')  ";
      $albSqlOrderBy = " `artist` ASC ";
   }
   
   //
   //
   // Here we could add an explode on "," and a for/while clause to do "and"/"or" searching
   //
   //
   
   
   if ($status && ($status !== 'all') ) $albSqlWhere .= " AND (`status`='".$status."') ";
   if ($cat && ($cat !== 'all') ) $albSqlWhere .= " AND ( (`category`='".$cat."') OR (`category2`='".$cat."') OR (`category3`='".$cat."') ) ";
   $albSql .= $albSqlWhere.' ORDER BY '.$albSqlOrderBy;
   $albQuery = mysql_query($albSql) or die(mysql_error());
   while ($alb = mysql_fetch_array($albQuery)) {
      printf('<span class="sidebarCollapsed" ><a onClick="parent.albInfoCtxt(\'alb\',%s);">%s<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;by %s</a></span><br />', $alb[cddbid], $alb[title], $alb[artist]);      
   }
} else if ($srchType === 'label') {
   $albSql = " SELECT * FROM `album` WHERE ";   
   $albSqlWhere = " (`label` LIKE '%".mysql_real_escape_string($s)."%') ";
   $albSqlOrderBy = " `label`,`artist` ASC ";
   if ($status && ($status !== 'all') ) $albSqlWhere .= " AND (`status`='".$status."') ";
   if ($cat && ($cat !== 'all') ) $albSqlWhere .= " AND ( (`category`='".$cat."') OR (`category2`='".$cat."') OR (`category3`='".$cat."') ) ";
   $albSql .= $albSqlWhere.' ORDER BY '.$albSqlOrderBy;
   $albQuery = mysql_query($albSql) or die (mysql_error());
   while ($alb = mysql_fetch_array($albQuery)) {
      if (strtolower(trim($alb[label])) !== strtolower(trim($oldLabel))) echo('<b>'.$alb[label].'</b><br />');
      printf('<span class="sidebarCollapsed" ><a onClick="parent.albInfoCtxt(\'alb\',%s);">%s<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;by %s</a></span><br />', $alb[cddbid], $alb[title], $alb[artist]);
      $oldLabel = $alb[label];
   }
} else if ($srchType === 'slot') {
   ?><span style="font-weight: bold;">Slot Numbers (<?php echo($s) ?>):</span><br /><?php 
   
   $slotSql = " SELECT `slot`,`artist`,`title`,`cddbid` FROM `album` WHERE ";

   $s = str_replace(' ', '', $s);
   
   if (in_string(',', $s)) {
      $s = explode(',', $s);
   } else {
      $sTmp = $s;
      unset ($s);
      $s[0] = $sTmp;
//      $s[0] = $s;
   }
   for ($j=0; $j < count($s); $j++) {
      if ($s[$j]) {
         if ($slotSqlWhere) {
            $slotSqlWhere .= " OR (";
            $closeOr = true;
         }
            if (in_string('-', $s[$j])) {
            $slot = explode('-', $s[$j]);
            $slotSqlWhere .= " (`slot`>='".$slot[0]."') AND (`slot`<='".$slot[1]."') ";
         } else {
            $slotSqlWhere .= " (`slot`='".$s[$j]."') ";
         }
         if ($closeOr) $slotSqlWhere .= ") ";
      }   
   }
   if ($slotSqlWhere) {
      if ($cat && ($cat !== 'all') ) $slotSqlWhere .= " AND ( (`category`='".$cat."') OR (`category2`='".$cat."') OR (`category3`='".$cat."') ) ";
      if ($status && ($status !== 'all') ) $slotSqlWhere .= " AND (`status`='".$status."') ";
      $slotSql = $slotSql . $slotSqlWhere;
      $slotSql .= " ORDER BY `slot` ASC ";
      //echo($slotSql);
      $slotQuery = mysql_query($slotSql);
      
      while ($slot = mysql_fetch_array($slotQuery)) {
         printf('<span class="sidebarCollapsed" ><a onClick="parent.albInfoCtxt(\'alb\', \'%s\');">%s: %s <br /> by %s</span></a><br />', $slot[cddbid], $slot[slot], $slot[title], $slot[artist]);
      }
   } else {
      echo('Enter slot numbers to search for, separated by commas or by a dash (-) to search in ranges.');
   }
} else if ($srchType === 'albId') {
   ?><span style="font-weight: bold;">album ID's(<?php echo($s) ?>):</span><br /><?php 
   
   $slotSql = " SELECT `cddbid`,`slot`,`artist`,`title`,`cddbid` FROM `album` WHERE ";

   $s = str_replace(' ', '', $s);
   
   if (in_string(',', $s)) {
      $s = explode(',', $s);
   } else {
      $sTmp = $s;
      unset ($s);
      $s[0] = $sTmp;
//      $s[0] = $s;
   }
   for ($j=0; $j < count($s); $j++) {
      if ($s[$j]) {
         if ($slotSqlWhere) {
            $slotSqlWhere .= " OR (";
            $closeOr = true;
         }
            if (in_string('-', $s[$j])) {
            $slot = explode('-', $s[$j]);
            $slotSqlWhere .= " (`cddbid`>='".$slot[0]."') AND (`cddbid`<='".$slot[1]."') ";
         } else {
            $slotSqlWhere .= " (`cddbid`='".$s[$j]."') ";
         }
         if ($closeOr) $slotSqlWhere .= ") ";
      }   
   }
   if ($slotSqlWhere) {
      if ($cat && ($cat !== 'all') ) $slotSqlWhere .= " AND ( (`category`='".$cat."') OR (`category2`='".$cat."') OR (`category3`='".$cat."') ) ";
      if ($status && ($status !== 'all') ) $slotSqlWhere .= " AND (`status`='".$status."') ";
      $slotSql = $slotSql . $slotSqlWhere;
      $slotSql .= " ORDER BY `slot` ASC ";
      //echo($slotSql);
      $slotQuery = mysql_query($slotSql);
      
      while ($slot = mysql_fetch_array($slotQuery)) {
         printf('<span class="sidebarCollapsed" ><a onClick="parent.albInfoCtxt(\'alb\', \'%s\');">%s: %s <br /> by %s</span></a><br />', $slot[cddbid], $slot[cddbid], $slot[title], $slot[artist]);
      }
   } else {
      echo('Enter album IDs to search for, separated by commas or by a dash (-) to search in ranges.');
   }
}
?>
</body>
</html>